<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class products extends Admin_Controller
{
	var $_table_name = PRODUCTS;
	var $_thetopic_table = PRODUCT_GROUPS;
	function lists()
	{
		global $CI;
		//check language
		$group = $CI->uri->segment(3)?$CI->uri->segment(3):-1;
		$language="";
		if($language=="english"){
			$this->_table_name = $this->_table_name."_en";			
		}
		// model
		$CI->load->module_model('products.products_list_model');
		// sql
		if($group>0){
			$q = $CI->db->get_where($this->_table_name, array(PRODUCTS_PRODUCT_ID=>$group));
		}
		else
			$q = $CI->db->get($this->_table_name);
		
		// pagenavigation
		$CI->load->library('pagination');
        $config['base_url'] = site_url($CI->products_list_model->url_prefix.'/'.$group.'/%s');
        $config['total_rows'] = $q->num_rows();
        $config['per_page'] = $CI->config->item('rows_per_page');
		$config['cur_page'] = $CI->products_list_model->start;
		$config['uri_segment'] = $CI->products_list_model->start;
		$config['num_links'] = $CI->config->item('num_links');		
        $CI->pagination->initialize($config);
        $this->add('paging', $CI->pagination->create_links());
		
		// sql
		$CI->db->select('p.*, p_g.'.PRODUCT_GROUP_NAME.' as goup_name,p_c.'.PRODUCT_CATEGORIES_NAME.' as product_cate_name ');
		$CI->db->from($this->_table_name.' p');		
		$CI->db->join(PRODUCT_GROUPS.' p_g', 'p.'.PRODUCTS_GROUP.' = p_g.'.PRODUCT_GROUP_ID);
		$CI->db->join(PRODUCT_CATEGORIES.' p_c', 'p_g.'.PRODUCT_GROUP_CATEGORIES.' = p_c.'.PRODUCT_CATEGORIES_ID);		    	
		if($group>0){
			$CI->db->where('p_g.'.PRODUCT_GROUP_ID, $group);		
		}
		foreach ($CI->products_list_model->columns as $k=>$v)
		{
			if ($CI->products_list_model->cache->load($k))
				$CI->db->order_by($v, $CI->products_list_model->cache->load($k));
		}
		$CI->db->limit($CI->config->item('rows_per_page'), $CI->products_list_model->start);
		$q = $CI->db->get();		
		$items = $q->result();
		
		//get groups
		$CI->db->select('p_g.*,p_c.'.PRODUCT_CATEGORIES_NAME.' as product_cate_name ');
		$CI->db->from($this->_thetopic_table.' p_g');		
		$CI->db->join(PRODUCT_CATEGORIES.' p_c', 'p_g.'.PRODUCT_GROUP_CATEGORIES.' = p_c.'.PRODUCT_CATEGORIES_ID);			
		$CI->db->order_by('p_c.'.PRODUCT_CATEGORIES_ID);		
		$g = $CI->db->get();	
		$groups = $g->result();
		//end get
		
		//Get image path
		$CI->load->library("fckeditor");	
		$path = $CI->fckeditor->UploadPath.'/images/';
		$this->add('image_franchise_path', $path);
						
		foreach($items as &$item){
			$item->image_path = $path.'/thumb/'.$item->{PRODUCTS_IMAGE};
		}
		// gridview
		$this->add('items', $items);
		$this->add('start', $CI->products_list_model->start);
		$this->add('language', $language?$language:'');
		foreach ($CI->products_list_model->columns as $k=>$v)
		{
			if ($CI->products_list_model->cache->load($k))
				$this->add('sort_'.$k,  'class="sort_column_'.$CI->products_list_model->cache->load($k).'"');
		}

		// message
		$this->add('msg', $CI->products_list_model->cache->load($CI->products_list_model->msg_cache_prefix));
		$CI->products_list_model->cache->add($CI->products_list_model->msg_cache_prefix, '');
		
		// toolbar
		$this->module_toolbar('products.toolbar', array('parent_id'=>0, 'start'=>$CI->products_list_model->start, 'language'=>$language, 'groups'=>$groups, 'group'=>$group));
		$this->module_navigation('Quản lý sản phẩm ');
		
		// view
		$this->view('products_list_view');
	}
	
	function detail()
	{
		global $CI;
		//check language
		$language = $CI->uri->segment(3);
		if($language=="english"){
			$this->_table_name = $this->_table_name."_en";			
			$this->_thetopic_table .= "_en";
		}
		// model
		$CI->load->module_model('products.products_detail_model');
		
		// Get All topic
		//$q = $CI->db->get($this->_thetopic_table);
		//$groups = $q->result();
		$CI->db->select('p_g.*,p_c.'.PRODUCT_CATEGORIES_NAME.' as product_cate_name ');
		$CI->db->from($this->_thetopic_table.' p_g');		
		$CI->db->join(PRODUCT_CATEGORIES.' p_c', 'p_g.'.PRODUCT_GROUP_CATEGORIES.' = p_c.'.PRODUCT_CATEGORIES_ID);	
		$CI->db->order_by('p_c.'.PRODUCT_CATEGORIES_ID);
		$q = $CI->db->get();	
		$groups = $q->result();
		
		$q = $CI->db->get_where($this->_table_name, array(PRODUCTS_PRODUCT_ID=>$CI->products_detail_model->item));
		$products = $q->row();
		
			
		// error
		$post = $CI->products_detail_model->cache->load('post');
		$CI->products_detail_model->cache->add('post', '');

		// add to view
		$this->add('name', $post?$post['name']:($products?$products->{PRODUCTS_NAME}:''));
		$this->add('group', $post?$post['group']:($products?$products->{PRODUCTS_GROUP}:''));
		$this->add('price', $post?$post['price']:($products?$products->{PRODUCTS_PRICE}:''));
		$this->add('promotion_price', $post?$post['promotion_price']:($products?$products->{PRODUCTS_PROMOTION_PRICE}:''));
		$this->add('groups', $groups);
		$this->add('active', $post?(isset($post['active'])?' checked':''):($products?($products->{PRODUCTS_STATUS}?' checked':''):' checked'));
		$this->add('imagepath', $post?$post['imagepath']:($products?$products->{PRODUCTS_IMAGE}:''));
		$this->add('imagejson', $post?$post['imagejson']:($products?$products->{PRODUCTS_IMAGE_JSON}:''));
		
		$this->add('start', $CI->products_detail_model->start);
		$this->add('item', $CI->products_detail_model->item);
		$this->add('language', $language?$language:'');
		
				
		// editor
		$CI->load->library('fckeditor');
		$CI->fckeditor->InstanceName = 'description';
		$CI->fckeditor->Value = $post?$post['description']:($products?$products->{PRODUCTS_DESCRIPTION}:'');
		$CI->fckeditor->SetPathOfFranchise();
		$this->add('description', $CI->fckeditor->CreateHtml());
		
		// editor
		$CI->load->library('fckeditor');
		$CI->fckeditor->InstanceName = 'properties';
		$CI->fckeditor->Value = $post?$post['properties']:($products?$products->{PRODUCTS_PROPERTIES}:'');
		$CI->fckeditor->SetPathOfFranchise();
        $CI->fckeditor->Height = 140;
		$this->add('properties', $CI->fckeditor->CreateHtml());
				
		// message
		$this->add('msg', $CI->products_detail_model->cache->load($CI->products_detail_model->msg_cache_prefix));
		$CI->products_detail_model->cache->add($CI->products_detail_model->msg_cache_prefix, '');
		
		// toolbar
		$this->module_toolbar();
		if ($CI->products_detail_model->item)
			$this->module_navigation('Nhóm sản phẩm : HIệu chỉnh');
		else
			$this->module_navigation('Nhóm sản phẩm : Thêm mới');
			
		// view
		$this->view('products_detail_view');
	}
	
}

?>